﻿#include "udpsocket.h"
#include "systemfile.h"
extern bool mysql_check;
#define udp_send_port 5000
udpSocket::udpSocket(QObject *parent) :
    QObject(parent)
{
    dev_reboot=false;

}

QString getHostIpByWinSock()  //获取本机IP地址
{
    QString IP;
    QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
    for (int i = 0; i < ipAddressesList.size(); ++i)
    {
        if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
                ipAddressesList.at(i).toIPv4Address())
        {
            // 如果这个地址不是127.0.0.1，并且是IPv4的地址，就将其赋值给IP，并结束for循环
            IP = ipAddressesList.at(i).toString();
            break;
        }
    }
    // 如果IP为空，则将其赋值为127.0.0.1
    if (IP.isEmpty())
        IP = QHostAddress(QHostAddress::LocalHost).toString();
    return IP;
}

void udpSocket::udp_broadcast_reciver_data()
{
    QByteArray data;
    QHostAddress sender;
    quint16 senderPort;
    QString udpdata;
    if(udpBroadLink->hasPendingDatagrams())
    {
        QString askStr,localIp;
        QByteArray askBuffer;
        QString headStr;
        data.resize(udpBroadLink->pendingDatagramSize());
        udpBroadLink->readDatagram(data.data(),data.size(),&sender,&senderPort);
        QStringList list;
        localIp=getHostIpByWinSock();
        qDebug()<<"接收UDP消息："<<data.data();
        qDebug()<<"ip:"<<sender<<"port:"<<QString::number(senderPort)<<"localIp:"<<localIp;
        QTextCodec *codec = QTextCodec::codecForName("GB2312"); //转化编码
        udpdata = codec->toUnicode(data);
        if(udpdata.contains(","))
        {
            list= udpdata.split(',');
            qDebug()<<"ip:"<<sender<<"port:"<<QString::number(senderPort)<<"localIp:"<<localIp;
            headStr = "::" + systemFile::GetInstance()->mac;
            qDebug() << "list[0]=" << list[0];
            qDebug() << "list[1]=" << list[1];

            if(sender==QHostAddress(localIp))
            {
                qDebug()<<"sender==QHostAddress(systemFile::GetInstance()->localIp)"<<sender;
                return;
            }
            else
            {
                if(list[0] == "::FF:FF:FF:FF:FF:FF" && list[1] == "FIND")
                {

                    askStr = QString("%1,%2,%3,%4,%5\n").arg(headStr).arg("FIND").arg(systemFile::GetInstance()->galleryName).arg(systemFile::GetInstance()->galleryNumber).arg(localIp);
                    askBuffer = codec->fromUnicode(askStr);
                    udpBroadLink->writeDatagram(askBuffer,askBuffer.count(),QHostAddress("255.255.255.255"),systemFile::GetInstance()->udpRecivePort);
                    qDebug()<<"send="<<askStr;
                    return;
                }
                else if(list[1] == "REBOOT")
                {
                    askStr = QString("%1,REBOOT,OK").arg(headStr);
                    askBuffer = codec->fromUnicode(askStr);
                    udpBroadLink->writeDatagram(askBuffer,askBuffer.count(),QHostAddress("255.255.255.255"),systemFile::GetInstance()->udpRecivePort);
                    dev_reboot=true;
                    return;
                }
            }
        }else
        {
            if(data.contains("1"))
            {
                qDebug()<<"Just Recive MSG And Set Some Par";
                askStr = QString("Ok,I Recive You Msg = 1,i will reread the .ini file");
                askBuffer = codec->fromUnicode(askStr);
                udpBroadLink->writeDatagram(askBuffer,askBuffer.count(),sender,senderPort);



                //重新读取配置文件
                qDebug()<<"重新读取配置文件";
                if(systemFile::GetInstance()->rereadSettting())
                {
                    qDebug()<<"成功";
                    emit singalSetAxis();

                }
                return;
            }
            if(data.contains("2"))
            {
                qDebug()<<"Reboot";
                askStr = QString("Ok,I Recive You Msg = 2,i will reboot");
                askBuffer = codec->fromUnicode(askStr);
                udpBroadLink->writeDatagram(askBuffer,askBuffer.count(),sender,senderPort);
                dev_reboot=true;
                return;
            }

        }
    }
}


QString udpSocket::count_qont()
{
    QTime time;
    time= QTime::currentTime();
    qsrand(time.msec()+time.second()*1000);
    int a=qrand()%9;
    int b=qrand()%9;
    int c=qrand()%9;
    int d=qrand()%9;
    int e=qrand()%9;
    int f=qrand()%9;
    QString xxx=QString("%1%2%3%4%5%6").arg (a).arg (b).arg (c).arg (d).arg (e).arg (f);
    return xxx;
}


QString udpSocket::getXorEncrypt(QString str_in)
{
    QString temp[7],str_out;
    qDebug()<<"str_in"<<str_in;
    int div[7];
    temp[0]=str_in.mid (3,1);
    temp[1]=str_in.mid (1,1);
    temp[2]=str_in.mid (5,1);
    temp[3]=str_in.mid (0,1);
    temp[4]=str_in.mid (4,1);
    temp[5]=str_in.mid (2,1);
    qDebug()<<"temp"<<temp;
    div[0]=CharToInt(temp[3])+CharToInt(temp[5]);
    div[1]=CharToInt(temp[2])+CharToInt(temp[1]);
    div[2]=div[0]+CharToInt(temp[4]);
    div[3]=div[0]+div[1];
    div[4]=CharToInt(temp[5])+CharToInt(temp[4]);
    div[5]=div[3]+div[0];
    for(int i=0;i<6;i++)
    {
        QString str;
        int out=getXor(div[i]);
        str=IntToChar(out);
        str_out.append (str);
    }
    qDebug()<<"str_out"<<str_out;
    return str_out;
}

int udpSocket::getXor(int m)
{
    int n;
    if(m>15)
    {
        div_t temp;
        temp=div(m,16);
        n=temp.rem;
    }else{
        n=m;
    }
    return n;
}

int udpSocket::CharToInt(QString n)
{
    if (n=="0")
        return 0;
    if (n=="1")
        return 1;
    if (n=="2")
        return 2;
    if (n=="3")
        return 3;
    if (n=="4")
        return 4;
    if (n=="5")
        return 5;
    if (n=="6")
        return 6;
    if (n=="7")
        return 7;
    if (n=="8")
        return 8;
    if (n== "9")
        return 9;
    if (n=="A")
        return 10;
    if (n=="B")
        return 11;
    if (n== "C")
        return 12;
    if (n=="D")
        return 13;
    if (n=="E")
        return 14;
    if (n=="F")
        return 15;

}

QString udpSocket::IntToChar(int n)
{
    switch(n) {
    case 0:return "0";break;
    case 1:return "1";break;
    case 2:return "2";break;
    case 3:return "3";break;
    case 4:return "4";break;
    case 5:return "5";break;
    case 6:return "6";break;
    case 7:return "7";break;
    case 8:return "8";break;
    case 9:return "9";break;
    case 10:return "A";break;
    case 11:return "B";break;
    case 12:return "C";break;
    case 13:return "D";break;
    case 14:return "E";break;
    case 15:return "F";break;
    }
}

